﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XsqTech.SmartOA.Domain.Table;
using XsqTech.SmartOA.EFCore.Respository;
using XsqTech.SmartOA.IRespository;

namespace XsqTech.SmartOA.Service.Tasks
{
    public class SchedulerMonitor : ISchedulerListener
    {
        ITimingRespository _baseTiming;
        ITimingLogRespository _timingLog;
        readonly IUnitOfWork _unitOfWork;

        public SchedulerMonitor(ITimingRespository baseTiming, ITimingLogRespository timingLog, IUnitOfWork unitOfWork)
        {
            _baseTiming = baseTiming;
            _timingLog = timingLog;
            _unitOfWork = unitOfWork;
        }


        #region 集中式方法
        /// <summary>
        /// 添加日志的操作
        /// </summary>
        /// <param name="jobDetail"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        private bool FocusOn(string msg, DateTime Time)
        {
            TimingLog Logs = new TimingLog();


            Logs.Msg = msg;
            Logs.StartTime = Time;
            Logs.PGroupName = "";
            _timingLog.Add(Logs);
            var a = _unitOfWork.SaveChangs()>0;
            return a;

        }

        #endregion

        public Task JobAdded(IJobDetail jobDetail, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(true);
        }

        public Task JobDeleted(JobKey jobKey, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("JobDeleted", DateTime.Now));
        }

        public Task JobInterrupted(JobKey jobKey, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("JobInterrupted", DateTime.Now));
        }

        public Task JobPaused(JobKey jobKey, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("JobPaused", DateTime.Now));
        }

        public Task JobResumed(JobKey jobKey, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("JobResumed", DateTime.Now));
        }

        public Task JobScheduled(ITrigger trigger, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("JobScheduled", DateTime.Now));
        }

        public Task JobsPaused(string jobGroup, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("JobsPaused", DateTime.Now));
        }

        public Task JobsResumed(string jobGroup, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("JobsResumed", DateTime.Now));
        }

        public Task JobUnscheduled(TriggerKey triggerKey, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("JobUnscheduled", DateTime.Now));
        }

        public Task SchedulerError(string msg, SchedulerException cause, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("SchedulerError", DateTime.Now));
        }

        public Task SchedulerInStandbyMode(CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("SchedulerInStandbyMode", DateTime.Now));
        }

        public Task SchedulerShutdown(CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("SchedulerShutdown", DateTime.Now));
        }

        public Task SchedulerShuttingdown(CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("SchedulerShuttingdown", DateTime.Now));
        }

        public Task SchedulerStarted(CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("SchedulerStarted", DateTime.Now));
        }

        public Task SchedulerStarting(CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("SchedulerStarting", DateTime.Now));
        }

        public Task SchedulingDataCleared(CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("SchedulingDataCleared", DateTime.Now));
        }

        public Task TriggerFinalized(ITrigger trigger, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("TriggerFinalized", DateTime.Now));
        }

        public Task TriggerPaused(TriggerKey triggerKey, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("TriggerPaused", DateTime.Now));
        }

        public Task TriggerResumed(TriggerKey triggerKey, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("TriggerResumed", DateTime.Now));
        }

        public Task TriggersPaused(string? triggerGroup, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("TriggersPaused", DateTime.Now));
        }

        public Task TriggersResumed(string? triggerGroup, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(FocusOn("TriggersResumed", DateTime.Now));
        }
    }
}
